define(function(require, exports, module) {
    // 通过 require 引入依赖
    var jqValidate = require('jqueryValidate');
    var jqForm = require('jqueryForm');
    var bootstrapJs = require('bootstrap');
    var common = require('common');
    var bootstrapTpl = require('bootstrapTpl');

    var validateForm = $("#form1").validate({
        errorElement:'div',
        errorClass : 'clearfix help-block invalid-feedback',
        validClass:' is-valid',
        rules:{
            uname:{required:true,rangelength: [6,20]},
            password:{required:true,rangelength: [5,20]},
            passwordCheck:{equalTo: "#password",required:true,rangelength: [5,20]},
            userRealName:{required:true,rangelength: [2,12]},
            legal:{required:true,rangelength: [2,12]},
            contactName:{required:true,rangelength: [2,12]},
            company:{required:true},
            blankMan:{required:true},
            blankNumber:{required:true},
            blank:{required:true},
            userTel:{required:true,rangelength: [11,11]},
            xyNumber:{required:true,rangelength: [16,20]},
            txtAuthCode:{required:true,rangelength: [4,4]},
            inputPic1:{required:true},
            inputPic2:{required:true},
            inputPic3:{required:true},
            inputPic4:{required:true},
            inputPic5:{required:true},
            inputPic6:{required:true}
        },
        messages:{
            uname:{
                required: "账号不能为空",
                rangelength:'账号必须为6-20位'
            },
            password:{
                required: "密码不能为空",
                rangelength:'密码长度必须在5-20位'
            },
            passwordCheck:{
                required: "确认密码不能为空",
                rangelength:'确认密码长度必须在5-20位'
            },
            userRealName:{
                required: "姓名不能为空",
                rangelength:'姓名长度不正确'
            },legal:{
                required: "法人姓名不能为空",
                rangelength:'法人姓名长度不正确'
            },contactName:{
                required: "单位联系人不能为空",
                rangelength:'单位联系人姓名长度不正确'
            },
            userTel:{
                required: "手机不能为空",
                rangelength:'手机格式不正确'
            },
            company:{
                required: "单位名称不能为空"
            },blankMan:{
                required: "户名不能为空"
            },blankNumber:{
                required: "开户行账号不能为空"
            },blank:{
                required: "开户银行名称不能为空"
            },xyNumber:{
                required: "统一社会信用代码证号不能为空",
                rangelength:'统一社会信用代码证号格式不正确'
            },
            txtAuthCode:{
                required: "验证码不能为空",
                rangelength:'验证码长度不正确'
            },
            inputPic1:{
                required: "营业执照不能为空"
            },
            inputPic2:{
                required: "开户许可证扫描图片不能为空"
            },
            inputPic3:{
                required: "法人代表身份证不能为空"
            },
            inputPic4:{
                required: "法人代表授权书不能为空"
            },
            inputPic5:{
                required: "管理员身份证扫描图片不能为空"
            },
            inputPic6:{
                required: "保证金回单不能为空"
            }
        },
        //自定义错误消息放到哪里
        errorPlacement : function(error, element) {
            myValidate.errorPlacement(error, element);
        },
        //给未通过验证的元素进行处理
        highlight : function(element) {
            myValidate.highlight(element);
        },
        //验证通过的处理
        success : function(label) {
            myValidate.success(label);
        }
    });

    var subCheck = {
        initialize:function(){
            this.initControl();
            //common.verificationCode('regAuthCode');//验证码刷新
        },
        initControl:function(){
            var fn = this;
            $('#form1').submit(function(){
                $(this).ajaxSubmit({
                    type: "post",
                    beforeSubmit: $.proxy(validateForm.form, validateForm),
                    success:fn.subSuccess,
                    clearForm:false
                });
                return false;//此处必须返回false，阻止常规的form提交
            });
            $("#form1").keydown(function(e){
                if(e.keyCode == 13){
                    $("#form1").click();
                }
            });
            $('.goback').click(function(){
                history.go(-1);
            })
        },
        subSuccess:function(result){
            //$("#regSub").attr('disabled',false);

            result = jQuery.parseJSON(result);
            if(result && result.status=='200'){
                $('.alert').remove();
                $("#form1").after( bootstrapTpl.alertClass('注册成功', 1) );
                window.location.href="/login";
                return false;
            }else{
                //console.log('登录失败',result.msg);
                var msg = '';
                if( typeof result.msg=='object' ){
                    $.each( result.msg , function(i, n){
                        msg += n + '<br/>';
                    });
                }else{
                    msg = result.msg;
                }
                $('.alert').remove();
                $("#form1").after(  bootstrapTpl.alertClass(msg, 4)  );
                return false;
            }
        },

    }
    subCheck.initialize();
});